package Tcp;

import android.content.Intent;
import android.util.Log;

import com.shengchang.hu.supervision.UI.InBoundActivity;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;




/**
 * Created by Administrator on 2017/2/15.
 */
public class TcpClient extends Thread {
    private String TAG = "TcpClient";
    private String serverIP = "192.168.88.141";
    private int serverPort = 1234;
    private PrintWriter pw;
    private InputStream is;
    private DataInputStream dis;
    private boolean isRun = true;
    private Socket socket = null;
    byte buff[]  = new byte[1];
    private String rcvMsg;
    private int rcvLen;



    public TcpClient(String ip , int port){
        this.serverIP = ip;
        this.serverPort = port;
    }

    public void closeSelf(){

     if (socket!=null){
        isRun = false;

     }else if (socket ==null){
         Log.e("suudie", "未建立连接");
     }
    }

    public  void close(){
        if (socket !=null){
            try {
                socket.close();
                socket=null;
                isRun=false;
            } catch (IOException e) {
            }
        }else if (socket ==null){
            Log.e("dheuha", "未建立连接");
        }
    }
    public void send(String msg){
        pw.println(msg);
        pw.flush();
    }

    @Override
    public void run() {
        StringBuffer str = new StringBuffer();
        try {
                socket = new Socket(serverIP, serverPort);
                socket.setSoTimeout(50);
                pw = new PrintWriter(socket.getOutputStream(), true);
                is = socket.getInputStream();
                dis = new DataInputStream(is);
                } catch (IOException e) {
            e.printStackTrace();
            }

        String weight = "";

        while (isRun){
            try {
                rcvLen = dis.read(buff);
                Log.i(TAG, "run: 收到消息:"+ rcvMsg);
                str.append(new String(buff,0,rcvLen,"utf-8"));
                char[] strW = str.toString().toCharArray();
                Log.d("dzczc",str.toString());
                if (strW[strW.length-1]=='g'){
                    String rep = str.toString().replace(" kg","").replace("ST"+","+"NT","").replace(" ","");
                    String[] spl = rep.split(",");

                    weight = spl[spl.length-1];
                        rcvMsg = weight;
                        Intent intent =new Intent();
                        intent.setAction("tcpClientReceiver");
                        intent.putExtra("tcpClientReceiver",rcvMsg);
                      InBoundActivity.context.sendBroadcast(intent);//将消息发送给主界面
                        if (rcvMsg.equals("QuitClient")){   //服务器要求客户端结束
                            isRun = false;
                        }
                    }else {
                        continue;
                    }

                    } catch (IOException e) {
                e.printStackTrace();
            }

        }
        try {
            pw.close();
            is.close();
            dis.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
